package com.eonmind.knowledge.core;

import com.eonmind.common.contract.enums.knowledge.FileUploadStatusEnum;
import com.eonmind.common.contract.exception.BusinessException;
import com.eonmind.knowledge.dto.KnowledgeUploadFileDto;
import com.eonmind.knowledge.service.KnowledgeUploadFileService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @Author: AKang
 * @Description:
 * @CreateTime: 2025-06-06
 */
@Slf4j
@Service
public class AsyncTaskService {
    @Resource
    private BailianKnowledgeService bailianKnowledgeService;
    @Resource
    private KnowledgeUploadFileService knowledgeUploadFileService;

    @Async("uploadTaskExecutor")
    public void importToBailianAsync(KnowledgeUploadFileDto.KnowledgeFile knowledgeFile) {
        try {

            // 导入到百炼知识库
            String realName = bailianKnowledgeService.importDocumentsToBailian(knowledgeFile.getIndexId(), knowledgeFile.getFileUrl());
            knowledgeFile.setRealName(realName);
            knowledgeFile.setFileStatus(FileUploadStatusEnum.UPLOAD_SUCCESSFULLY.getCode());
            knowledgeUploadFileService.updateKnowledgeUploadFile(knowledgeFile);
        } catch (Exception e) {
            log.error("知识库文件导入异常：{}", e.getMessage(), e);
            throw new BusinessException(e.getMessage());
        }
    }
}
